package edu.umn.contactviewer;

import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.provider.ContactsContract.Profile;
import android.util.Log;

public class GetContactsTask extends AsyncTask<String, Void, Contacts> {
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		// create a progress dialog
	}

	String URL_BASE = "http://contacts.tinyapollo.com/";
	String API_KEY = "idroid";

	@Override
	protected Contacts doInBackground(String... params) {
		try {
			AndroidHttpClient client = AndroidHttpClient.newInstance("Android",
					null);
			HttpUriRequest request = new HttpGet(URL_BASE + "contacts/"
					+ "?key=" + API_KEY);
			HttpResponse response = client.execute(request);
			Gson gson = new Gson();

			Contacts result = gson.fromJson(new InputStreamReader(response
					.getEntity().getContent()), Contacts.class);
			client.close();
			return result;
		} catch (Exception ex) {
			Log.w("GetContactTask", "Error getting contact", ex);
		}
		return null;
	}

	@Override
	protected void onPostExecute(Contacts result) {
		super.onPostExecute(result);
		// close the progress dialog and tell the activity that you've received
		// a result
	}

}